import config from './config'
import request from './request'
import classnames from 'classnames'
import {notification, Modal} from 'antd'
import {color} from './theme'

// import mockStorge from './mockStorge'
// require('./mock.js') //如果不使用mock模拟API将该行代码屏蔽掉

// 连字符转驼峰
String.prototype.hyphenToHump = function () {
  return this.replace(/-(\w)/g, function () {
    return arguments[1].toUpperCase()
  })
};

// 驼峰转连字符
String.prototype.humpToHyphen = function () {
  return this.replace(/([A-Z])/g, "-$1").toLowerCase()
};

// 数据容量单位转换
Number.prototype.bytesToSize = function () {
  if (this === 0) return '0 b';
  let k = 1024, // or 1024
    sizes = ['b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb'],
    i = Math.floor(Math.log(this) / Math.log(k));
  return (this / Math.pow(k, i)).toPrecision(3) + sizes[i];
};

// 日期格式化
Date.prototype.format = function (format) {
  var o = {
    "M+": this.getMonth() + 1,
    "d+": this.getDate(),
    "h+": this.getHours(),
    'H+': this.getHours(),
    "m+": this.getMinutes(),
    "s+": this.getSeconds(),
    "q+": Math.floor((this.getMonth() + 3) / 3),
    "S": this.getMilliseconds()
  }
  if (/(y+)/.test(format))
    format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length))
  for (var k in o)
    if (new RegExp("(" + k + ")").test(format))
      format = format.replace(RegExp.$1, RegExp.$1.length == 1
        ? o[k]
        : ("00" + o[k]).substr(("" + o[k]).length))
  return format
};

//全局列表分页参数
window.queryApiInitPageSize = 10;
const parseParams = (data) => {
  let params = '';

  if (data) {
    for (var o in data) {
      if (data[o] !== null && data[o] !== undefined && data[o] !== '') {

        if (typeof data[o] === 'object' && data[o] instanceof Array && data[o].length > 0) {
          if (typeof data[o][0] === 'object') {
            params += (o + '=' + JSON.stringify(data[o]) + '&')
          } else {
            for (var i = 0; i < data[o].length; i++) {
              params += (o + '=' + data[o][i] + '&')
            }
          }
        } else {
          params += (o + '=' + data[o] + '&')
        }
      }
    }
  }

  if (params.length > 0) {
    params = params.substr(0, params.length - 1);
    //params = '?'+params+'_dc='+Math.random();
  }
  /*else{
   params = '?_dc='+Math.random();
   }*/
  return decodeURI(params);
};
const openNotice = (type, message, desc) => {
  notification[type]({
    message: message,
    description: desc,
  });
};
const openModal = (type, title, msg) => {
  Modal[type]({
    title: title,
    content: msg,
    okText: '关闭'
  });
};
const str2json = (str) => {
  let t = null;
  try {
    t = JSON.parse(str);
  } catch (e) {
    t = eval("(" + str + ')');
  } finally {
    return t;
  }
};
const isNull = (x) => {
  if (x === null || x === undefined || x === '') {
    return true;
  }
  return false;
};
const formatMoney = (num) => {//带千分位的整数
  var num = (num || 0).toString()
    , result = ''
    , dec = ''
    , temp = '';
  if (num.match('.')) {
    var t = num.split('.');
    num = t[0];
    if (t.length > 1)
      dec = t[1];
  }
  while (num.length > 3) {
    result = ',' + num.slice(-3) + result;
    num = num.slice(0, num.length - 3);
  }
  if (num) {
    result = num + result;
  }
  while (dec.length > 3) {
    temp = dec.slice(0, 3) + ',' + temp;
    dec = dec.slice(3);
  }
  if (dec) {
    temp = temp + dec;
  } else {
    if (temp)
      temp = temp.slice(0, temp.length - 2);
  }
  return ( dec ? (result + '.' + temp) : result);
};

module.exports = {
  config,
  color,
  classnames,
  isNull:isNull,
  request,
  str2json,
  openModal,
  openNotice,
  parseParams,
  formatMoney
}
